package com.yunji.order.task;

import com.yunji.common.utils.TimeUtils;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.order.bo.ListDeliveryOrderBO;
import com.yunji.order.bo.ListScaleOrderBO;
import com.yunji.order.bo.OrderUnFrozenBO;
import com.yunji.order.business.OrderBusiness;
import com.yunji.order.context.convert.OrderConvert;
import com.yunji.order.domain.OrderDO;
import com.yunji.order.emuns.OrderStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @version 1.0
 * @Project: yunji-coffee
 * @Package com.yunji.order.compensate
 * @FileName DelayTask.java
 * @Copyright Copyright © 2024 云集互联. All Rights Reserved
 * Company		com
 * @CreateTime 2024-05-11 14:43:26
 * @Description 延时补偿处理
 * @Modification Author                    History Time			Version				Description
 * --------------------------------------------------------------------------------
 * HuaAo0803@163.com	2024-05-11 14:43:26		1.0					1.0 Version
 * @since JDK 1.8.0_202
 */
@Component("delayTask")
public class DelayTask {
    private static final Logger logs = LoggerFactory.getLogger(DelayTask.class);

    @Resource
    OrderBusiness orderBusiness;

    /**
     * @Title closeOrder
     * @Desc 定时关闭订单
     * @Date 2024-05-11 16:17:46.417
     */
    public void closeOrder() {
        List<OrderDO> list = orderBusiness.searchOrderStatus(OrderStatusEnum.WAIT_PAYING.getCode(), OrderStatusEnum.ORDER_CANCEL.getCode());
        if (BeanUtils.isNull(list)) {
            return;
        }
        for (OrderDO order : list) {
            logs.info("【定时关闭订单】订单信息:{}", order);
            Long orderId = order.getOrderId();
            try {
                long createTime = order.getCreateTime().getTime();
                long currentTime = System.currentTimeMillis();
                logs.info("【定时关闭订单】当前时间：{},创建时间:{}", currentTime, createTime);

                long timestamp = currentTime - createTime;
                logs.info("【自动关闭订单】自动关闭时差{}", timestamp);
                if (timestamp < 300000) {
                    continue;
                }
                //orderBusiness.closedOrder(order);
                logs.info("【定时关闭订单】订单{}关闭成功", orderId);
            } catch (Exception e) {
                logs.error("【定时关闭订单】订单{}关闭失败", orderId, e);
            }
        }

    }

    /**
     * @Title deliveries
     * @Desc 定时订单发货
     * @Date 2024-05-11 16:18:08.698
     */
    public void deliveries() {
        List<OrderDO> list = orderBusiness.sendProductOrder(new ListDeliveryOrderBO(OrderStatusEnum.PAY_SUCCESS.getCode(),
                0, TimeUtils.toDate(LocalDateTime.now().minusDays(1L))));
        if (BeanUtils.isNull(list)) {
            return;
        }
        for (OrderDO order : list) {
            logs.info("【定时订单发货】订单信息:{}", order);
            Long orderId = order.getOrderId();
            try {
                orderBusiness.orderDeliveries(order);
                logs.info("【定时订单发货】订单{}发货成功", orderId);
            } catch (Exception e) {
                logs.error("【定时订单发货】订单{}发货失败", orderId, e);
            }
        }
    }

    public void orderScale() {
        ListScaleOrderBO getScaleOrderBO = new ListScaleOrderBO(OrderStatusEnum.PAY_SUCCESS.getCode(), 0,
                TimeUtils.toDate(LocalDate.now().minusDays(2L)));
        List<OrderDO> list = orderBusiness.searchScaleOrder(getScaleOrderBO);
        if (BeanUtils.isNull(list)) {
            return;
        }
        for (OrderDO order : list) {
            logs.info("【定时订单分润】订单信息:{}", order);
            Long orderId = order.getOrderId();
            try {
                OrderUnFrozenBO orderScaleBO = OrderConvert.buildOrderScaleBO(order);
                orderBusiness.unFrozenScale(orderScaleBO);
                logs.info("【定时订单分润】订单{}清算成功", orderId);
            } catch (Exception e) {
                logs.error("【定时订单分润】订单{}清算失败", orderId, e);
            }
        }
    }
}